package cn.badlion.toolbox.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Environment;

import java.io.File;
import java.lang.reflect.Method;

import cn.badlion.toolbox.debug.LOG;
import dalvik.system.DexClassLoader;

public class LoadApk {

    private void loadPlugin(Activity activity) {
        String pluginFolder = Environment.getExternalStorageDirectory() + "/download/plugin.apk";
        File file = new File(pluginFolder);
        if (file.exists()) {
            PackageInfo packageInfo = activity.getPackageManager().getPackageArchiveInfo(file.getPath(),
                    PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES);
            if (packageInfo == null) {
                return;
            }
            String launcherActivityName = null;
            if (packageInfo.activities != null && packageInfo.activities.length > 0) {
                launcherActivityName = packageInfo.activities[0].name;
            }

            Intent intent = new Intent();
            intent.setPackage(packageInfo.packageName);
            intent.setClassName(packageInfo.packageName, launcherActivityName);
            activity.startActivity(intent);
//            DexClassLoader dexClassLoader = createDexClassLoader(file.getPath());
//            AssetManager assetManager = createAssetManager(file.getPath());
//            Resources resources = createResources(assetManager);
        }
    }
    private DexClassLoader createDexClassLoader(Activity activity,String dexPath) {
        File dexOutputDir = activity.getDir("dex", Context.MODE_PRIVATE);
        String dexOutputPath = dexOutputDir.getAbsolutePath();
        String libPath = activity.getDir("lib", Context.MODE_PRIVATE).getAbsolutePath();
        return new DexClassLoader(dexPath, dexOutputPath, libPath, activity.getClassLoader());
    }

    private AssetManager createAssetManager(String dexPath) {
        try {
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
            addAssetPath.invoke(assetManager, dexPath);
            return assetManager;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    private Resources createResources(Activity activity,AssetManager assetManager) {
        Resources superRes = activity.getResources();
        return new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
    }


    @SuppressLint("UnsafeDynamicallyLoadedCode")
    public static void loadLib(Activity activity, String packageName, String libName){
        try {
            String nativeLibraryDir = activity.getPackageManager().getApplicationInfo(packageName, 0).nativeLibraryDir;
            System.load(nativeLibraryDir + libName);
        }catch (Exception e){
            LOG.DebugError(e);
        }
    }

}